package mop.gui.task.focuswork;

import java.util.LinkedList;
import javax.swing.JButton;
import javax.swing.table.TableCellRenderer;
import mop.controller.FocusWorkController;
import mop.gui.GUI;
import mop.gui.component.JTableButtonModel;
import mop.gui.component.JTableButtonRenderer;
import mop.model.task.TaskInterface;
import mop.model.task.tasktype.TaskTypeInterface;

/**
 * Deadline based focus.
 *
 * @author  MOP Team 7
 */
public class TaskTypeBasedFocusPanel extends javax.swing.JPanel implements FocusWorkInterface
{
    /**
     * Empty constructor.
     */
    public TaskTypeBasedFocusPanel()
    {
        initComponents();
    }

    /**
     * Constructor.
     *
     * @param   FocusWorkPanel parent
     * @param   gui
     * @param   removeProjectController
     * @effect  Sets the parent panel.
     * @effect  Sets gui object.
     * @effect  Sets controller(s).
     */
    public TaskTypeBasedFocusPanel(FocusWorkPanel parent, GUI gui, FocusWorkController focusWorkController)
    {
        initComponents();
        this.parent = parent;
        this.setGui(gui);
        this.focusWorkController = focusWorkController;

        // Load task types
        for (TaskTypeInterface t : focusWorkController.getAllTaskTypes())
            this.cmbTaskType.addItem(t.getId());
    }

    /***************************************************************************
     *                          GUI reference
     ***************************************************************************/

    private GUI gui;

    /**
     * Sets the GUI.
     *
     * @param   gui
     * @post    gui is set.
     */
    private void setGui(GUI gui)
    {
        this.gui = gui;
    }

    /**
     * Returns the GUI reference.
     *
     * @return  this.gui
     */
    public GUI getGui()
    {
        return this.gui;
    }

    /***************************************************************************
     *                             Focus Work
     ***************************************************************************/

    private FocusWorkPanel parent;
    private FocusWorkController focusWorkController;

     /**
     * Enables a user to focus on tasks based on their deadline.
     */
    public void focus()
    {
        JTableButtonModel tableModel = new JTableButtonModel();

        String[] columns = {"Task", "Status", "Type", "Details", "Change Status"};

        Object[][] rows;

        try {
            LinkedList<TaskInterface> focusTasks = new LinkedList<TaskInterface>();
            focusTasks.addAll( focusWorkController.getTaskTypeBasedFocus(this.cmbTaskType.getSelectedItem().toString()) );
            rows = parent.generateFocusWorkRows(gui, focusTasks, FocusWorkPanel.FocusType.TASKTYPE);
        } catch (Throwable t) {
            rows = new Object[1][5];
            rows[0][0] = "";
            rows[0][1] = "";
            rows[0][2] = "";
            rows[0][3] = "";
            rows[0][4] = "";
        }

        tableModel.setColumns(columns);
        tableModel.setRows(rows);

        tableTaskTypeBased.setModel(tableModel);
        TableCellRenderer defaultRenderer = tableTaskTypeBased.getDefaultRenderer(JButton.class);
        JTableButtonRenderer buttonRenderer = new JTableButtonRenderer(defaultRenderer);
        tableTaskTypeBased.setDefaultRenderer(JButton.class, buttonRenderer);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        tableTaskTypeBased = new javax.swing.JTable();
        lblTaskType = new javax.swing.JLabel();
        cmbTaskType = new javax.swing.JComboBox();

        setPreferredSize(new java.awt.Dimension(517, 569));

        tableTaskTypeBased.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(tableTaskTypeBased);

        lblTaskType.setText("Task Type");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 497, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblTaskType)
                        .addGap(18, 18, 18)
                        .addComponent(cmbTaskType, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 521, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblTaskType)
                    .addComponent(cmbTaskType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cmbTaskType;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblTaskType;
    private javax.swing.JTable tableTaskTypeBased;
    // End of variables declaration//GEN-END:variables

    public String getTitle() {
        return "Task Type";
    }

}